package com.sntk.organauthomanager.infrastructure.dao;

import com.sntk.organauthomanager.domain.model.Organization;
import com.sntk.organauthomanager.domain.repository.OrganizationRepository;
import com.sntk.organauthomanager.infrastructure.convert.OrganizationConvert;
import com.sntk.organauthomanager.infrastructure.db.entity.OrganizationEntity;
import com.sntk.organauthomanager.infrastructure.db.repository.OrganizationDBRepository;
import com.sntk.organauthomanager.infrastructure.exception.ObjectExistedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @ClassName: OrganizationRepositoryImpl
 * @author: wgchao
 * @createTime: 2020/7/20 4:37 PM
 * @Description: TODO
 */
@Component
public class OrganizationRepositoryImpl implements OrganizationRepository {
    @Autowired
    private OrganizationDBRepository organizationDBRepository;
    @Autowired
    private OrganizationConvert organizationConvert;

    @Override
    public String generateNumber(String superiorNumber) {
        if(StringUtils.isEmpty(superiorNumber)){
            OrganizationEntity organizationEntity =organizationDBRepository.
                    findFirstByInstitutionNumberNotAndSuperiorNumberIsNullOrderByInstitutionNumberDesc("3702");
            if(organizationEntity == null){
                return "3702830001";
            }
            String organNumber = organizationEntity.getInstitutionNumber();
            String indexStr = organNumber.substring(organNumber.length()-4, organNumber.length());
            indexStr = "0000"+(Integer.valueOf(indexStr) + 1);
            return "370283"+indexStr.substring(indexStr.length()-4, indexStr.length());
        }else {
            OrganizationEntity organizationEntity =organizationDBRepository.findFirstBySuperiorNumberOrderByInstitutionNumberDesc(superiorNumber);
            if(organizationEntity == null){
                return superiorNumber+"0001";
            }
            String organNumber = organizationEntity.getInstitutionNumber();
            String indexStr = organNumber.substring(organNumber.length()-4, organNumber.length());
            indexStr = "0000"+(Integer.valueOf(indexStr) + 1);
            return superiorNumber+indexStr.substring(indexStr.length()-4, indexStr.length());
        }
    }

    @Override
    public void add(Organization organization, String operator) {
        OrganizationEntity organizationEntity = organizationDBRepository.findFirstByInstitutionNumberAndDelFalse(organization.getInstitutionNumber());
        if(organization.isAdd()){
            if(organizationEntity != null){
                throw new ObjectExistedException("机构编号已被占用");
            }
            organizationEntity = organizationConvert.convert(organization);
            organizationEntity.setDel(false);
            organizationEntity.setCreateTime(new Date());
            organizationEntity.setCreator(operator);
            organizationEntity.setLastUpdateTime(new Date());
            organizationEntity.setLastUpdator(operator);
            organizationEntity = organizationDBRepository.save(organizationEntity);
        }else{
            if(organizationEntity == null){
                throw new ObjectExistedException("机构不存在");
            }
            OrganizationEntity newEntity = organizationConvert.convert(organization);
            newEntity.setDel(organizationEntity.getDel());
            newEntity.setCreateTime(organizationEntity.getCreateTime());
            newEntity.setCreator(organizationEntity.getCreator());
            newEntity.setLastUpdateTime(new Date());
            newEntity.setLastUpdator(operator);
            newEntity = organizationDBRepository.save(newEntity);
        }
    }

    @Override
    public Organization findByKey(String s) {
        OrganizationEntity organizationEntity = organizationDBRepository.findFirstByInstitutionNumberAndDelFalse(s);
        if(organizationEntity == null){
            return null;
        }
        return organizationConvert.convert(organizationEntity);
    }

    @Override
    public void delete(String s, String operator) {
        OrganizationEntity organizationEntity = organizationDBRepository.findFirstByInstitutionNumberAndDelFalse(s);
        if(organizationEntity == null){
            throw new ObjectExistedException("机构不存在");
        }
        organizationEntity.setDel(true);
        organizationEntity.setLastUpdateTime(new Date());
        organizationEntity.setLastUpdator(operator);
        organizationEntity = organizationDBRepository.save(organizationEntity);
    }
}
